package com.tanhua.sso.controller;

import com.tanhua.sso.service.UserInfoService;
import com.tanhua.sso.service.UserService;
import com.tanhua.sso.vo.ErrorResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: my-tanhua
 * @description:
 * @author: xuejingjing
 * @create: 2021-09-19 07:22
 **/
@RestController
@RequestMapping("user")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;


    /**
     * 完善个人信息-基本信息
     * @param param
     * @return  提交的是json数据，所有需要@RequestBody接受，请求头里面的token
     */
    @PostMapping("loginReginfo")
    public ResponseEntity<Object> saveUserInfo(@RequestBody Map<String,String> param, @RequestHeader("Authorization") String token){
        try {
            Boolean bool = userInfoService.saveUserInfo(param,token);
            if(bool){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        ErrorResult errorResult = ErrorResult.builder().errorCode("000001").errorMessage("保存用户信息失败").build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }


    /**
     * 上传照片
     * @param param
     * @param token
     * @return
     */
    @PostMapping("loginReginfo")
    public ResponseEntity<Object> saveUserLogo(@RequestParam("headPhoto") MultipartFile multipartFile, @RequestHeader("Authorization") String token){
        try {
            Boolean bool = userInfoService.saveUserLogo(multipartFile,token);
            if(bool){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        ErrorResult errorResult = ErrorResult.builder().errorCode("000001").errorMessage("保存用户信息失败").build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }
    
}